// pages/myCollectPatient/myCollectPatient.js
Page({
  data: {
    admin: '',
    collect: [], //收藏的医生的id
    doctorInfo: [], //医生信息
  },
  onLoad(options) {
    var that = this
    //一进来就获取这个人的账号
    that.setData({
      admin: options.admin
    })
    //利用传进来的账号查询该账号的收藏信息
    wx.cloud.callFunction({
      name: 'collect',
      data: {
        action: 'findPatient',
        patientAdmin: options.admin
      }
    }).then(res => {
      console.log('获取收藏信息成功', res)
      //把获取到的收藏信息数组转成只有医生id的对象
      let collectTmp = res.result.data.map(item => {
        return item.doctorId
      })
      this.setData({
        collect: collectTmp
      })
      console.log('获取云数据后本地的收藏信息', this.data.collect)
      //获取医师信息
      wx.cloud.callFunction({
        name: 'doctorInfo',
        data: {
          action: 'find'
        }
      }).then(res => {
        console.log('获取医师信息成功', res)
        that.setData({
          doctorInfo: res.result.data
        })
      }).catch(res => {
        console.log('获取医师信息失败', res)
      })
    }).catch(res => {
      console.log('获取收藏信息失败', res)
    })
  },
  //点击医生图片显示医师执照
  showDoctorPic: function (e) {
    console.log("这个医生的图片数组", e.currentTarget.dataset.doctorpic)
    var imgUrl = e.currentTarget.dataset.doctorpic
    wx.previewImage({
      urls: imgUrl, //需要预览的图片http链接列表，注意是数组
    })
  },
  //取消收藏
  delCollect(e) {
    var that = this
    wx.cloud.callFunction({
      name: 'collect',
      data: {
        action: 'del',
        patientAdmin: this.data.admin,
        doctorId: e.currentTarget.dataset.doctorid
      }
    }).then(res => {
      console.log('删除这条收藏信息成功', res)
      //从新获取
      wx.cloud.callFunction({
        name: 'collect',
        data: {
          action: 'findPatient',
          patientAdmin: that.data.admin
        }
      }).then(res => {
        console.log('获取收藏信息成功', res)
        //把获取到的收藏信息数组转成只有医生id的对象
        let collectTmp = res.result.data.map(item => {
          return item.doctorId
        })
        that.setData({
          collect: collectTmp
        })
        wx.showToast({
          title: '取消收藏成功',
        })
      })
    }).catch(res => {
      console.log('删除这条收藏信息失败', res)
      wx.showToast({
        icon: 'none',
        title: '取消收藏失败',
      })
    })
  }
})